package com.estate.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.auth.service.AuthService;
import com.estate.bean.Manager;
import com.estate.config.JwtConfig;
import com.estate.mapper.ManagerMapper;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

@Service
public class AuthServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements AuthService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public JsonResult login(Manager manager) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();

       /* wrapper.eq("password", manager.getPassword());
        wrapper.last(" and (login_name='"+manager.getLoginName()+"' or email='"+manager.getLoginName()+"')");
*/
        wrapper.eq("password",manager.getPassword())
                .and(e->e.eq("login_name",manager.getLoginName())
                        .or().eq("email",manager.getLoginName())
                        .or().eq("telephone",manager.getLoginName())
                );
        Manager one = getOne(wrapper);
        if (one == null) {
            return ResultTool.fail(401, "账号或者密码错误");
        }
        String token = JwtConfig.getJwtToken(one);
        stringRedisTemplate.opsForValue().set("token:fcy" + one.getId(), token, JwtConfig.EXPIRE, TimeUnit.MILLISECONDS);
        JsonResult success = ResultTool.success(token);
        success.setError(one.getLoginName());
        return success;
    }
}
